package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BaseGroup;
import com.ruoyi.base.service.IBaseGroupService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 组Controller
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Api(description="组管理")
@RestController
@RequestMapping("/base/baseGroup")
public class BaseGroupController extends BaseController
{
    @Autowired
    private IBaseGroupService baseGroupService;

    /**
     * 查询组列表
     */
    @ApiOperation("查询组列表")
    @GetMapping("/list")
    public TableDataInfo list(BaseGroup baseGroup)
    {
        baseGroup.setUnitCode(getLoginUser().getUser().getUnit());
        List<BaseGroup> list = baseGroupService.selectBaseGroupList(baseGroup);
        return getDataTable(list);
    }

    /**
     * 导出组列表
     */
    @ApiOperation("导出组列表")
    @Log(title = "组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseGroup baseGroup)
    {
        List<BaseGroup> list = baseGroupService.selectBaseGroupList(baseGroup);
        ExcelUtil<BaseGroup> util = new ExcelUtil<BaseGroup>(BaseGroup.class);
        util.exportExcel(response, list, "组数据");
    }

    /**
     * 获取组详细信息
     */
    @ApiOperation("获取组详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(baseGroupService.selectBaseGroupByID(ID));
    }

    /**
     * 新增组
     */
    @ApiOperation("新增组")
    @Log(title = "组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseGroup baseGroup)
    {
        baseGroup.setID(UUID.randomUUID().toString().replace("-", ""));
        baseGroup.setCreatedBy(getUserId().toString());
        baseGroup.setCreatedTime(new Date());
        baseGroup.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(baseGroupService.insertBaseGroup(baseGroup));
    }

    /**
     * 修改组
     */
    @ApiOperation("修改组")
    @Log(title = "组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseGroup baseGroup)
    {
        baseGroup.setUpdatedBy(getUserId().toString());
        baseGroup.setUpdatedTime(new Date());
        baseGroup.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(baseGroupService.updateBaseGroup(baseGroup));
    }

    /**
     * 删除组
     */
    @ApiOperation("删除组")
    @Log(title = "组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(baseGroupService.deleteBaseGroupByIDs(IDs));
    }
}
